/**
 * 日期工具类
 */

/**
 * 格式化日期
 * @param {Date|number|string} date 日期对象、时间戳或日期字符串
 * @param {string} format 格式化模式，例如 'YYYY-MM-DD HH:mm:ss'
 * @returns {string} 格式化后的日期字符串
 */
export function formatDate(date, format = 'YYYY-MM-DD') {
  if (!date) return ''
  
  // 转换为日期对象
  const dateObj = new Date(date)
  if (isNaN(dateObj.getTime())) {
    return ''
  }
  
  const year = dateObj.getFullYear()
  const month = dateObj.getMonth() + 1
  const day = dateObj.getDate()
  const hours = dateObj.getHours()
  const minutes = dateObj.getMinutes()
  const seconds = dateObj.getSeconds()
  
  // 格式化映射
  const map = {
    YYYY: year.toString(),
    YY: year.toString().slice(-2),
    MM: month.toString().padStart(2, '0'),
    M: month.toString(),
    DD: day.toString().padStart(2, '0'),
    D: day.toString(),
    HH: hours.toString().padStart(2, '0'),
    H: hours.toString(),
    mm: minutes.toString().padStart(2, '0'),
    m: minutes.toString(),
    ss: seconds.toString().padStart(2, '0'),
    s: seconds.toString()
  }
  
  return format.replace(/YYYY|YY|MM|M|DD|D|HH|H|mm|m|ss|s/g, matched => map[matched])
}

/**
 * 获取相对时间描述
 * @param {Date|number|string} date 日期对象、时间戳或日期字符串
 * @returns {string} 相对时间描述，例如"刚刚"、"5分钟前"等
 */
export function getRelativeTime(date) {
  if (!date) return ''
  
  const dateObj = new Date(date)
  if (isNaN(dateObj.getTime())) {
    return ''
  }
  
  const now = new Date()
  const diff = (now - dateObj) / 1000 // 转换为秒
  
  if (diff < 60) {
    return '刚刚'
  } else if (diff < 3600) {
    return Math.floor(diff / 60) + '分钟前'
  } else if (diff < 86400) {
    return Math.floor(diff / 3600) + '小时前'
  } else if (diff < 2592000) {
    return Math.floor(diff / 86400) + '天前'
  } else if (diff < 31536000) {
    return Math.floor(diff / 2592000) + '个月前'
  } else {
    return Math.floor(diff / 31536000) + '年前'
  }
}

/**
 * 获取日期范围
 * @param {string} type 范围类型：today, week, month, year
 * @returns {Array} 开始日期和结束日期的数组
 */
export function getDateRange(type) {
  const now = new Date()
  let start, end
  
  switch (type) {
    case 'today':
      start = new Date(now.setHours(0, 0, 0, 0))
      end = new Date(now.setHours(23, 59, 59, 999))
      break
    case 'week':
      const day = now.getDay() || 7 // 将周日的0转为7
      start = new Date(now.setDate(now.getDate() - day + 1))
      start.setHours(0, 0, 0, 0)
      end = new Date(now.setDate(now.getDate() + 6))
      end.setHours(23, 59, 59, 999)
      break
    case 'month':
      start = new Date(now.getFullYear(), now.getMonth(), 1)
      end = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999)
      break
    case 'year':
      start = new Date(now.getFullYear(), 0, 1)
      end = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999)
      break
    default:
      start = new Date(now.setHours(0, 0, 0, 0))
      end = new Date(now.setHours(23, 59, 59, 999))
  }
  
  return [start, end]
}

/**
 * 日期比较
 * @param {Date|number|string} date1 日期1
 * @param {Date|number|string} date2 日期2
 * @returns {number} -1表示date1早于date2，0表示相等，1表示date1晚于date2
 */
export function compareDate(date1, date2) {
  const d1 = new Date(date1).getTime()
  const d2 = new Date(date2).getTime()
  
  if (isNaN(d1) || isNaN(d2)) {
    return NaN
  }
  
  if (d1 < d2) {
    return -1
  } else if (d1 > d2) {
    return 1
  } else {
    return 0
  }
}

export default {
  formatDate,
  getRelativeTime,
  getDateRange,
  compareDate
} 